import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:zywz/config/app_colors.dart';

import '../../dialog/dialog1.dart';
import '../../models/services_data.dart';

// 直接使用AppColors静态类

class ServiceOrderDetailPage extends StatefulWidget {
  const ServiceOrderDetailPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _ServiceOrderDetailPageState();
  }

}

class _ServiceOrderDetailPageState extends State<ServiceOrderDetailPage> {

  OrderBean? orderBean;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final state = GoRouterState.of(context);
      setState(() {
        orderBean = state.extra as OrderBean?;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    // 模拟服务订单数据

    return Scaffold(
      appBar: AppBar(
        centerTitle: true, // 这行是关键
        backgroundColor: AppColors.primaryColor,
        title: const Text(
          '服务订单详情',
          style: TextStyle(
            color: AppColors.textWhite,
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () => Navigator.pop(context),
        ),
      ),
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            SingleChildScrollView(
              child: showDetail(),
            ),
          ],
        ),
      ),
    );
  }


  Widget showDetail(){
    if(orderBean == null){
      return Center(
        child: Text('订单详情加载中...'),
      );
    }else{
      return  Container(
        padding: const EdgeInsets.all(16.0),
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // 表格标题
            // Text('服务订单详情', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
            SizedBox(height: 16),
            // 表格内容
            Text('订单编号：${orderBean?.orderNo}'),
            SizedBox(height: 5),
            Text('服务名称：${orderBean?.serviceName}'),
            SizedBox(height: 5),
            Text('公司名称：${orderBean?.companyName}'),
            SizedBox(height: 5),
            Text('服务费用：${orderBean?.amount}元'),
            SizedBox(height: 5),
            Text('交易流程号：${orderBean?.transactionId ?? ''}'),
            SizedBox(height: 5),
            Text('购买时间：${formatTimestamp(orderBean?.gmtCreate??'')}'),
            SizedBox(height: 5),
            // Text('交易状态：${orderBean?.tradeType}'),
          ],
        ),
      );
    }



  }



// 将时间戳转换为日期格式
  String formatTimestamp(String timestampStr) {
    if (timestampStr == null || timestampStr.isEmpty) {
      return '';
    }

    try {
      int timestamp = int.parse(timestampStr);
      DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
      return DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
    } catch (e) {
      return timestampStr;
    }
  }


  void showPopDialog(BuildContext context, String contextText, {
    required Function() onConfirm,
    required Function() onCancel,
  }) {
    showDialog(
      context: context,
      barrierDismissible: false, // 点击外部不可关闭
      builder: (BuildContext context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.0),
          ),
          elevation: 0,
          backgroundColor: Colors.white,
          child: Container(
            padding: const EdgeInsets.all(24.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                const Text(
                  '提示',
                  style: TextStyle(
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 24.0),
                Text(contextText,
                  style: TextStyle(
                    fontSize: 16.0,
                    color: Color(0xFF333333),
                  ),
                  textAlign: TextAlign.center,
                ),
                const SizedBox(height: 32.0),
                Row(
                  children: [
                    Expanded(
                      child: SizedBox(
                        height: KHighLight,
                        child: ElevatedButton(
                          onPressed: onCancel,
                          style: ElevatedButton.styleFrom(
                            backgroundColor: const Color(0xFFE0F7FA), // 浅青色
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(25),
                            ),
                            elevation: 0,
                          ),
                          child: const Text('取消', style: TextStyle(color: Color(0xFF41C7DD), fontSize: 13)),
                        ),
                      ),
                    ),
                    const SizedBox(width: 16),
                    Expanded(
                      child: SizedBox(
                        height: KHighLight,
                        child: ElevatedButton(
                          onPressed: onConfirm,
                          style: ElevatedButton.styleFrom(
                            backgroundColor: const Color(0xFF41C7DD),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(25),
                            ),
                            elevation: 2,
                          ),
                          child: const Text('确认', style: TextStyle(color: Colors.white, fontSize: 13)),
                        ),
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
        );
      },
    );
  }




}
